package com.hockeo.tinygds;

import java.lang.reflect.Field;

import com.hockeo.tinygds.error.TNotLoadableException;

/**
 *
 * @version $Id$
 * @author jjanke
 */
public class TFieldEnum extends TField<Enum<?>>
{
  protected TFieldEnum( TinyGDSFactory factory, Field field )
  {
    super( factory, field );
  }

  @Override
  public Object getValueForDatastore( Object objInstance )
  {
    Enum<?> enumval = getValue( objInstance );

    if ( enumval == null )
      return null;

    return enumval.name();
  }

  @SuppressWarnings( { "unchecked", "rawtypes" } )
  @Override
  protected Enum<?> transformDatastoreValue( Object objDatastoreValue )
  {
    if ( objDatastoreValue instanceof String )
      return Enum.valueOf( (Class<Enum>) getRealField().getType(), (String) objDatastoreValue );

    throw TNotLoadableException.cannotConvertValue( objDatastoreValue, getRealField() );
  }
}
